package cn.getech.data.metadata.db.data;

import cn.getech.data.metadata.constant.ConnectTypeEnum;
import cn.getech.data.metadata.db.DataView;
import cn.getech.data.metadata.entity.meta.CollectionTableInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service("dataViewMysqlImpl")
public class DataViewMysqlImpl implements DataView {

    private Integer type;

    @Override
    public boolean support(Integer type) {
        this.type = type;
        return ConnectTypeEnum.MySQL.getCode().equals(type) ||
                ConnectTypeEnum.Hive.getCode().equals(type) ||
                ConnectTypeEnum.PostgreSQL.getCode().equals(type);
    }

    @Override
    public String getSql(CollectionTableInfo table, List<String> fieldList, Integer page, Integer limit) {
        String sql = "select `" + String.join("`,`", fieldList) + "`  from  %s.%s limit %d, %d";
        sql = String.format(sql, table.getDbName(), table.getTableName(), page-1, limit);
        return sql;
    }

    @Override
    public String getCountSql(CollectionTableInfo table) {
        String sql = "select  count(*) as totalCount  from  %s.%s  ";
        if (ConnectTypeEnum.Hive.getCode().equals(type)) {
            //hive 统计语法很慢  默认查询10000条
            sql = "select  10000  as totalCount  from  %s.%s  ";
        }
        return String.format(sql, table.getDbName(), table.getTableName());
    }
}
